package com.wl.common.system.controller;


import com.wl.common.system.service.impl.SysUserService;
import com.wl.common.core.constant.ConstantSystem;
import com.wl.common.core.web.dto.DataStoreDTO;
import com.wl.common.core.web.dto.RestResultDTO;
import com.wl.common.system.service.SysRoleService;
import com.wl.common.system.support.dto.*;
import com.wl.common.system.support.dto.query.SysUserQueryDTO;
import com.wl.common.system.support.vo.SysUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author: wanglin
 * @date: 2023-05-30 周二
 * @Version: 1.0
 * @Description:
 */
@Tag(name = "用户", description = "SysUserController")
@Validated
@CrossOrigin
@RestController
@RequestMapping(ConstantSystem.PROJECT_SYS_URL_PREFIX + "user")
public class SysUserController {
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysRoleService roleService;

    @Operation(summary = "分页")
    @RequestMapping(value = "page", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<DataStoreDTO<SysUserVO>> page(@ParameterObject @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                       @ParameterObject SysUserQueryDTO queryDto) {
        return RestResultDTO.newSuccess(userService.page(pageable, queryDto));
    }

    @Operation(summary = "分页")
    @RequestMapping(value = "dataPage", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<DataStoreDTO<SysUserVO>> dataPage(@ParameterObject @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                           @ParameterObject SysUserQueryDTO queryDto) {
        return RestResultDTO.newSuccess(userService.dataPage(pageable, queryDto));
    }

    @Operation(summary = "列表")
    @RequestMapping(value = "list", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<List<SysUserVO>> list(@ParameterObject @SortDefault(sort = "createTime", direction = Sort.Direction.DESC) Sort sort,
                                               @ParameterObject SysUserQueryDTO queryDto) {
        return RestResultDTO.newSuccess(userService.list(sort, queryDto));
    }

    @Operation(summary = "保存")
    @RequestMapping(value = "save", method = {RequestMethod.POST})
    public RestResultDTO<Void> save(@Validated @RequestBody SysUserDTO dto) {
        dto.setId(null);
        userService.save(dto);
        return RestResultDTO.newSuccess();
    }

    @Operation(summary = "新增用户")
    @RequestMapping(value = "addUser", method = {RequestMethod.POST})
    public RestResultDTO<Void> addUser(@Validated @RequestBody SysUserDTO dto) {
        dto.setId(null);
        userService.addUser(dto);
        return RestResultDTO.newSuccess();
    }

    @Operation(summary = "修改")
    @RequestMapping(value = "update", method = {RequestMethod.POST})
    public RestResultDTO<Void> update(@Validated @RequestBody SysUserDTO dto) {
        userService.update(dto);
        return RestResultDTO.newSuccess();
    }

    @Operation(summary = "修改用户")
    @RequestMapping(value = "updateUser", method = {RequestMethod.POST})
    public RestResultDTO<Void> updateUser(@Validated @RequestBody SysUserDTO dto) {
        userService.updateUser(dto);
        return RestResultDTO.newSuccess();
    }

    @Operation(summary = "删除")
    @RequestMapping(value = "delete", method = {RequestMethod.GET})
    public RestResultDTO<Void> delete(@RequestParam Set<String> ids) {
        userService.delete(ids);
        return RestResultDTO.newSuccess(null, "删除成功");
    }

    @Operation(summary = "查看")
    @RequestMapping(value = "get", method = {RequestMethod.GET})
    public RestResultDTO<SysUserVO> get(@Parameter(description = "记录ID") @RequestParam String id) {
        return RestResultDTO.newSuccess(userService.get(id));
    }

    //    @PreAuthorize("hasAnyAuthority('ADMIN')")
//    @Operation(summary = "启动或禁用用户userId——管理员")
//    @RequestMapping(value = "updateStatusById", method = {RequestMethod.GET})
//    public RestResultDTO<Void> updateStatusById(@Parameter(description = "用户Id") @RequestParam String userId) {
//        userService.updateStatusById(userId);
//        return RestResultDTO.newSuccess();
//    }

    @Operation(summary = "分配用户角色——管理员")
    @RequestMapping(value = "assignRolesById", method = {RequestMethod.GET, RequestMethod.POST})
    public RestResultDTO<Void> assignRolesById(@RequestBody UserRoleDTO dto) {
        userService.assignRolesById(dto);
        return RestResultDTO.newSuccess();
    }

    /**
     * 查询已分配用户角色列表
     */
    @Operation(summary = "查询已分配用户角色列表")
    @RequestMapping(value = "/allocatedList", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<DataStoreDTO<SysUserVO>> allocatedList(@ParameterObject @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                                @ParameterObject SysUserQueryDTO queryDto) {
        return RestResultDTO.newSuccess(userService.selectAllocatedList(pageable, queryDto));
    }

    /**
     * 查询未分配用户角色列表
     */
    @Operation(summary = "查询未分配用户角色列表")
    @RequestMapping(value = "/unallocatedList", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<DataStoreDTO<SysUserVO>> unallocatedList(@ParameterObject @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                                  @ParameterObject SysUserQueryDTO queryDto) {
        return RestResultDTO.newSuccess(userService.selectUnallocatedList(pageable, queryDto));
    }

    /**
     * 取消授权用户/批量取消授权用户
     */
    @Operation(summary = "取消授权用户/批量取消授权用户")
    @RequestMapping(value = "/cancelAuthUsers", method = {RequestMethod.POST, RequestMethod.PUT})
    public RestResultDTO<Void> cancelAuthUsers(@RequestBody SysUserQueryDTO queryDto) {
        userService.cancelAuthUsers(queryDto.getRoleId(), queryDto.getUserIds());
        return RestResultDTO.newSuccess(null, "取消授权成功");
    }

    @Operation(summary = "批量选择用户授权")
    @RequestMapping(value = "/accreditBatchUser", method = {RequestMethod.POST, RequestMethod.PUT})
    public RestResultDTO<Void> selectAuthUserAll(@RequestBody SysUserQueryDTO queryDto) {
        roleService.checkRoleDataScope(queryDto.getRoleId());
        userService.accreditBatchUser(queryDto.getRoleId(), queryDto.getUserIds());
        return RestResultDTO.newSuccess(null, "授权成功");
    }

    @Operation(summary = "修改个人信息")
    @RequestMapping(value = "updateUserProfile", method = {RequestMethod.POST})
    public RestResultDTO<Void> updateUserProfile(@Validated @RequestBody UserProfileDTO dto) {
        userService.updateUserProfile(dto);
        return RestResultDTO.newSuccess();
    }
//
//    @Operation(summary = "用户修改个人密码")
//    @RequestMapping(value = "updateUserPwd", method = {RequestMethod.POST})
//    public RestResultDTO<Void> updateUserPwd(@Validated @RequestBody PasswordDTO dto) {
//        userService.updateUserPwd(dto);
//        return RestResultDTO.newSuccess();
//    }
//
//    @Operation(summary = "重置密码——管理员")
//    @RequestMapping(value = "resetUserPwd", method = {RequestMethod.PUT, RequestMethod.POST})
//    public RestResultDTO<Void> resetUserPwd(@Validated @RequestBody ResetPasswordDTO dto) {
//        userService.resetUserPwd(dto);
//        return RestResultDTO.newSuccess();
//    }

}
